/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DBuscarCaracter.java
 *
 * Created on 4/02/2010, 03:47:40 PM
 */
package ig.formato.gui.app.herramientas;

import javax.swing.JFrame;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.Utilities;

/**
 *
 * @author jsanchez
 */
public class DBuscarCaracter extends javax.swing.JDialog {

    private JTextComponent c=null;
    /** Creates new form DBuscarCaracter */
    public DBuscarCaracter( JTextComponent c ) {
        super(new JFrame(), false);
        this.c=c;
        initComponents();
        if(c.getSelectedText()!=null){
            this.text.setText(c.getSelectedText());
            this.text.setSelectionStart(0);
            this.text.setSelectionEnd(this.text.getText().length());
        }
        java.awt.Dimension dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Rectangle dimensionPantalla = getBounds();
        setLocation((dimensionScreen.width - dimensionPantalla.width) / 2, (dimensionScreen.height - dimensionPantalla.height) / 2);    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        text = new javax.swing.JTextField();
        UpLo = new javax.swing.JCheckBox();
        abajo = new javax.swing.JRadioButton();
        arriba = new javax.swing.JRadioButton();
        find = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(DBuscarCaracter.class, "DBuscarCaracter.jLabel1.text")); // NOI18N

        text.setText(org.openide.util.NbBundle.getMessage(DBuscarCaracter.class, "DBuscarCaracter.text.text")); // NOI18N
        text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textActionPerformed(evt);
            }
        });

        UpLo.setText(org.openide.util.NbBundle.getMessage(DBuscarCaracter.class, "DBuscarCaracter.UpLo.text")); // NOI18N

        buttonGroup1.add(abajo);
        abajo.setSelected(true);
        abajo.setText(org.openide.util.NbBundle.getMessage(DBuscarCaracter.class, "DBuscarCaracter.abajo.text")); // NOI18N

        buttonGroup1.add(arriba);
        arriba.setText(org.openide.util.NbBundle.getMessage(DBuscarCaracter.class, "DBuscarCaracter.arriba.text")); // NOI18N

        find.setText(org.openide.util.NbBundle.getMessage(DBuscarCaracter.class, "DBuscarCaracter.find.text")); // NOI18N
        find.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(text, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(arriba, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(abajo, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(UpLo)
                        .addGap(51, 51, 51)
                        .addComponent(find, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                        .addGap(29, 29, 29)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(abajo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(arriba))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(UpLo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(find)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void findActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findActionPerformed
        find();
    }//GEN-LAST:event_findActionPerformed

    private void textActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textActionPerformed
        find();
}//GEN-LAST:event_textActionPerformed


    private void find(){
        TextSearch.find(c, this.text.getText(), abajo.isSelected(), this.UpLo.isSelected());
    }
    //-------------- find actions ----------------------------
    public void actionPerformedSelectWork() {

        if (c == null) {
            return;
        }
        int pos = c.getCaretPosition();

        try {
            // Find start of word from caret
            int start = Utilities.getWordStart(c, pos);

            // Check if start precedes whitespace
            if (start < c.getDocument().getLength() && Character.isWhitespace(c.getDocument().getText(start, 1).charAt(0))) {
                // Check if caret is at end of word
                if (pos > 0 && !Character.isWhitespace(c.getDocument().getText(pos - 1, 1).charAt(0))) {
                    // Start searching before the caret
                    start = Utilities.getWordStart(c, pos - 1);
                } else {
                    // Caret is not adjacent to a word
                    start = -1;
                }
            }
            if (start != -1) {
                // A non-whitespace character follows start.
                // Find end of word from start.
                int end = Utilities.getWordEnd(c, start);

                // Set selection
                c.setSelectionStart(start);
                c.setSelectionEnd(end);
            }
        } catch (BadLocationException e) {
        }
    }

    public void actionPerformedSelectNextWork() {

        if (c == null) {
            return;
        }
        int pos = c.getSelectionStart();
        int len = c.getDocument().getLength();

        try {
            // Find start of next word from selection.
            // getNextWord() throws BadLocationException if no word follows pos.
            int start = Utilities.getNextWord(c, pos);

            // Find end of word from start
            int end = Utilities.getWordEnd(c, start);

            // Set selection
            c.setSelectionStart(start);
            c.setSelectionEnd(end);
        } catch (BadLocationException e) {
        }
    }

    public void actionPerformedSelectPrevWork() {

        if (c == null) {
            return;
        }
        int pos = c.getSelectionStart();
        int len = c.getDocument().getLength();

        try {
            // Find start of previous word from selection.
            // getPreviousWord() throws BadLocationException if no word precedes pos.
            int start = Utilities.getPreviousWord(c, pos);

            // Find end of word from start
            int end = Utilities.getWordEnd(c, start);

            // Set selection
            c.setSelectionStart(start);
            c.setSelectionEnd(end);
        } catch (BadLocationException e) {
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox UpLo;
    private javax.swing.JRadioButton abajo;
    private javax.swing.JRadioButton arriba;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton find;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField text;
    // End of variables declaration//GEN-END:variables
}
